<?php

namespace Admin\Controller;

class IndexController extends BaseController {

  var $adminObj;

  public function __construct() {
    parent::__construct();
    $this->adminObj = D('Admin');
    $sysname = M("Sys")->where(array("name"=>"sys_name"))->getField("value");
    $this->assign('sysname', $sysname);
  }

  public function index() {
    foreach($this->adminMenu as $item){
      header("location:{$item['url']}");
      die;
    }
  }

  /**
   * 用户登录\显示登录界面
   */
  public function login() {
    $isLogin = I("doSubmit");
    if (!empty($isLogin)) {
      $data['name'] = I("username");
      $data['password'] = I("password");
      if (!empty($data['name']) && !empty($data['password'])) {
        $data['password'] = md5($data['password']);
        $adminInfo = $this->adminObj->adminInfo($data);
        if($adminInfo['id'] > 0 ){
          session('adminId', $adminInfo['id']);
          $this->redirect('User/index');
        }else{
          $this->error('用户名密码错误');
          die;
        }
      } else {
        $this->error('请输入用户名密码');
        die;
      }
    } else {
      $this->display();
    }
  }



  /**
   * 管理员退出登录
   */
  public function logout(){
    session('adminId', NULL);
    $this->success('成功退出登录',U("Index/login"));
  }
  

}
